//package edu.sctu.yueding.adapter;
//
//import android.content.Context;
//import android.support.v7.widget.RecyclerView;
//import android.view.LayoutInflater;
//import android.view.View;
//import android.view.ViewGroup;
//import android.widget.ImageView;
//
//import java.util.ArrayList;
//
//import edu.sctu.yueding.R;
//
///**
// * Created by wangfuxin on 2016/11/7.
// */
//
//public class IndexNewsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
//    private Context mContext;
//
//    private static final int TYPE_ITEM = 0;
//    private static final int TYPE_FOOTER = 1;
//
//    public IndexNewsAdapter(Context mContext) {
//        this.mContext = mContext;
//    }
//
//    public void refresh(ArrayList<String> data) {
//
//        this.notifyDataSetChanged();
//
//    }
//
//    @Override
//    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//
//        if (viewType == TYPE_ITEM) {
//
//            View view = LayoutInflater.from(parent.getContext()).inflate(
//                    R.layout.item_index_fragment_listview_news_v2, null);
//
//            view.setLayoutParams(new RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT,
//                    RecyclerView.LayoutParams.WRAP_CONTENT));
//
//            return new ItemViewHolder(view);
//
//        } else if (viewType == TYPE_FOOTER) { // type == TYPE_FOOTER 返回footerView
//
//            View view = LayoutInflater.from(parent.getContext()).inflate(
//                    R.layout.xlistview_footer, null);
//
//            view.setLayoutParams(new RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT,
//                    RecyclerView.LayoutParams.WRAP_CONTENT));
//
//            return new FooterViewHolder(view);
//
//        }
//
//
//        return null;
//    }
//
//
//    @Override
//    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
//
//        if (holder instanceof ItemViewHolder) {
//
//
//
//        }
//
//
//    }
//
//    @Override
//    public int getItemViewType(int position) {
//        // 最后一个item设置为footerView
//        if (position + 1 == getItemCount()) {
//            return TYPE_FOOTER;
//        } else {
//            return TYPE_ITEM;
//        }
//    }
//
//    @Override
//    public int getItemCount() {
//        return 20;
//
//    }
//
//    class FooterViewHolder extends RecyclerView.ViewHolder {
//
//        public FooterViewHolder(View view) {
//            super(view);
//        }
//    }
//
//
//    class ItemViewHolder extends RecyclerView.ViewHolder {
//        ImageView imageView;
//
//        public ItemViewHolder(View itemView) {
//            super(itemView);
//            imageView = (ImageView) itemView.findViewById(R.id.item_news_img);
//        }
//    }
//}


package edu.sctu.yueding.adapter;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.List;

import edu.sctu.yueding.R;
import edu.sctu.yueding.activitys.ActivityInformationActivity;
import edu.sctu.yueding.bean.HomeListViewBean;
import edu.sctu.yueding.bean.IndexMatchesBean;
import edu.sctu.yueding.config.CacheConfig;
import edu.sctu.yueding.utils.DateUtils;
import edu.sctu.yueding.utils.JumpUtils;
import edu.sctu.yueding.utils.httputils.GetVolleyHttp;
import edu.sctu.yueding.utils.httputils.SharePreference;

/**
 * Created by wangfuxin on 2016/11/7.
 */

public class IndexMatchesAdapter extends BaseAdapter {
    private Context mContext;
    private View view;
    private LayoutInflater layoutInflater;
    private ViewHolder viewHolder;
    int i = 0;
    private List<IndexMatchesBean> data;


    public IndexMatchesAdapter(Context mContext, List<IndexMatchesBean> data) {
        this.mContext = mContext;
        this.data = data;
        layoutInflater = LayoutInflater.from(mContext);
    }

//    /**
//     * 刷新数据
//     *
//     * @param data
//     */
//    public void fresh(List<IndexMatchesBean> data, boolean isAddAll) {
//        Log.e("page", data.size() + "size");
//
//        if (isAddAll) {
//            this.data.addAll(data);
//        } else {
//            this.data = data;
//        }
//        notifyDataSetChanged();
//    }


    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
//        if (convertView == null) {
        view = layoutInflater.inflate(R.layout.item_index_fragment_listview_matchs_v2, null);

        viewHolder = new ViewHolder();

        viewHolder.imageView = (ImageView) view.findViewById(R.id.item_news_img);
        viewHolder.matchName = (TextView) view.findViewById(R.id.match_name);
        viewHolder.matchTime = (TextView) view.findViewById(R.id.match_time);
        viewHolder.day = (TextView) view.findViewById(R.id.day);
        viewHolder.hour = (TextView) view.findViewById(R.id.hour);
        viewHolder.fen = (TextView) view.findViewById(R.id.fen);
        viewHolder.jieshu = (TextView) view.findViewById(R.id.jieshu);
        viewHolder.linearLayout = (LinearLayout) view.findViewById(R.id.lay);
        viewHolder.now = System.currentTimeMillis();
//
//            view.setTag(viewHolder);//将viewHolder储存在view中
//        } else {
//            viewHolder = (ViewHolder) view.getTag();
//            view = convertView;
//        }

        GetVolleyHttp.getInstance((Activity) mContext).setImageLoader(viewHolder.imageView, R.drawable.photo_pictures_no,
                R.drawable.photo_pictures_no, data.get(position).getThumbnail());

        viewHolder.matchTime.setText(DateUtils.getStrTime(String.valueOf(data.get(position).getMatchStart())));
        i++;
        Log.e("jjjjjjjjj", String.valueOf(i));
        viewHolder.matchName.setText(data.get(position).getTitle());


        if (data.get(position).getEnrollEnd() < viewHolder.now) {
            //报名结束
            viewHolder.linearLayout.setVisibility(View.GONE);

            viewHolder.jieshu.setText("报名已结束");

        } else {
            long time = data.get(position).getEnrollEnd() - viewHolder.now;

            Long day = time / (1000 * 60 * 60 * 24);//天

            Long hour = (time / (1000 * 60 * 60)) % 24;//天


            Long f = (time / (1000 * 60)) % 60;


            viewHolder.day.setText(String.valueOf(day));
            viewHolder.hour.setText(String.valueOf(hour));
            viewHolder.fen.setText(String.valueOf(f));

        }

        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Bundle bundle = new Bundle();
                bundle.putString("matchContent", data.get(position).getContent());

                if (data.get(position).getEnrollEnd() < viewHolder.now) {
                    //报名结束

                    bundle.putInt("endTag", ActivityInformationActivity.MATCH_END);
                } else {

                    bundle.putInt("endTag", ActivityInformationActivity.MATCH_NO_END);

                }


                SharePreference.getInstance(mContext).setCache(CacheConfig.CACHE_MATCH_ID, data.get(position).getId() + "");

                if (data.get(position).getPrice() == 0.0f)
                    SharePreference.getInstance(mContext).setCache(CacheConfig.IS_PRICE, true);
                else SharePreference.getInstance(mContext).setCache(CacheConfig.IS_PRICE, false);
                JumpUtils.jumpActivity(mContext, ActivityInformationActivity.class, bundle);

            }
        });

        return view;
    }

    private void dateUils() {
        long currentTime = System.currentTimeMillis();

    }

    private class ViewHolder {
        ImageView imageView;
        TextView matchName, matchTime, day, hour, fen, jieshu;
        LinearLayout linearLayout;
        long now;
    }
}
